package com.wanda.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.BuildModel;
import com.wanda.model.BuildProModel;
import com.wanda.model.CustomerModel;
import com.wanda.model.JsonGeneralModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.ProjectModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.BuildService;
import com.wanda.service.ProjectService;
import com.wanda.util.DateFormat;

@Controller
@RequestMapping("qt/ht/build") 
public class BuildController {

	
	@Autowired
	private BuildService buildService;
	@Autowired
	private ProjectService projectService;
	// 添加楼栋信息
	@RequestMapping(value = "/addBuild.do")
	public @ResponseBody Map<String, Object> add(String build_name, int floor_count, String remark,
			String project_id, String delete_state,String create_time,String tel,HttpServletRequest request) {

		System.out.println("进入添加");

		BuildModel model = new BuildModel();
		Wd_UserModel user = (Wd_UserModel)request.getSession().getAttribute("bean");

		model.setBuild_name(build_name);
		model.setFloor_count(floor_count);
		model.setRemark(remark);
		model.setProject_id(project_id);
		model.setDelete_state("0");
        model.setCreate_time(DateFormat.getNowDate());
		model.setTel(user.getTel());
		System.err.println(":::::::::::::::::;"+model.getTel());
		System.err.println(";;;;;;;;;;;;;;;;;;"+model.getBuild_name());
		
		buildService.insertBuild(model);
		
		Map<String, Object> map = new HashMap<String, Object>();

		System.out.println("添加成功");

		map.put("msg", "成功");

		return map;
	}

	// 查询楼栋所有信息
	@RequestMapping(value = "/selectAll.do")
	public ModelAndView showAll(HttpServletRequest request ,HttpSession session) {
		
		System.out.println("进入楼栋查询");
		ModelAndView model1 = new ModelAndView();
		List<BuildProModel> buildlist = null;
		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		try {
			if("admin".equals(user.getTel())){
				buildlist = buildService.selectAll();
				System.out.println("记录长度1");
				model1.setViewName("/ht/listBuild.jsp");
				model1.addObject("buildlist", buildlist);
			}else{
				buildlist = buildService.selectAlll(user.getTel());
				System.err.println("buildlist:"+buildlist);
				System.out.println("记录长度2");
				model1.setViewName("/ht/listBuild.jsp");
				model1.addObject("buildlist", buildlist);

			}
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return model1;
	}

	// 遍历单元表（房间）里需要的字段并返回(楼栋名字)
	@RequestMapping(value = "/selectAlls.do")
	public @ResponseBody Map<String, Object> showAlls() {

		Map<String, Object> map = new HashMap<String, Object>();

		List<BuildProModel> model = null;
		model = buildService.selectAll();
		map.put("msg", model);

		return map;
	}
	
	
	
	// 修改楼栋信息
	@RequestMapping(value = "/updateBuild.do")
	public String update(BuildModel model) {

		System.out.println("进入修改" + model.getBuild_name());
		model.setUpdate_time(DateFormat.getNowDate());
		buildService.updateBuild(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buildList", map);
		return "redirect:/qt/ht/build/selectAll.do";

	}

	@RequestMapping(value = "/selectAllByAdd.do")
	public ModelAndView showAddAll(HttpServletRequest request,String munber) {
		ModelAndView model = new ModelAndView();
		List<ProjectModel> listp = null;
		List<ProjectModel> listpjm= new ArrayList<ProjectModel>();
		try {
	       listp = projectService.selectAll();
			Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
			for (ProjectModel p : listp) {
					if(p.getTel()!=null){
						if(p.getTel().equals(user.getTel())){
							listpjm.add(p);
							System.out.println("进入项目11111111111111111111111查询"+p.getTel());
							System.out.println("进入项目11111111111111111111111查询"+p.getProject_name());
						}	
					}
			}
					
		}
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
		    System.err.println("wo bu shi admin" +listpjm.size());
		    model.addObject("proj", listpjm);
		}else{
			System.err.println("wo  shi admin");
			model.addObject("proj", listp);
		}
		model.setViewName("/ht/addBuild.jsp");
		
		System.err.println(listp.size());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;

	}
	
	// 软删除一条记录
	@RequestMapping(value = "/deleteBuild.do")
	public String delete(BuildModel model,HttpServletRequest request) {

		System.out.println("进入删除");

		model.setDelete_time(DateFormat.getNowDate());
		model.setBuild_id(model.getBuild_id());
		Wd_UserModel be = (Wd_UserModel) request.getSession().getAttribute("bean");
	    model.setDelete_user(be.getName());
		
		int build = buildService.deleteBuild(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", build);
		return "redirect:/qt/ht/build/selectAll.do";
	}

	// 查询单条记录
	@RequestMapping(value = "/selectById.do")
	public ModelAndView selectById(String id,HttpServletRequest request) {
		ModelAndView model = new ModelAndView();
		List<ProjectModel> listp = null;
		List<ProjectModel> listpjm= new ArrayList<ProjectModel>();
		try {
	       listp = projectService.selectAll();
			Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
			for (ProjectModel p : listp) {
					if(p.getTel()!=null){
						if(p.getTel().equals(user.getTel())){
							listpjm.add(p);
							System.out.println("进入项目11111111111111111111111查询"+p.getTel());
							System.out.println("进入项目11111111111111111111111查询"+p.getProject_name());
						}	
					}
			}
					
		}
		if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
		    System.err.println("wo bu shi admin" +listpjm.size());
		    model.addObject("proj", listpjm);
		}else{
			System.err.println("wo  shi admin");
			model.addObject("proj", listp);
		}
		System.out.println("进入单条查询");
		
		BuildModel build = null;
		
			build = buildService.selectById(id);
			model.setViewName("/ht/updateBuild.jsp");
			model.addObject("build", build);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;
	}

	//拼接sql查询
   @RequestMapping(value="/selectBySqls.do")
	public ModelAndView selectBySql(HttpServletRequest request,int page,int rows,String SearchSql){
	    ModelAndView model1 = new ModelAndView();
	    List<BuildProModel> return_build =new ArrayList<BuildProModel>();
		List<BuildProModel> builds =new ArrayList<BuildProModel>();
	 
		System.err.println(page+"     孔           "+rows);
	    if(page<1){
			page=1;
		}
	    Wd_UserModel user = (Wd_UserModel)request.getSession().getAttribute("bean");
			
		int zjls=0;  //总记录数
		// rows=0;   //每页多少
		// page=1;   //当前页数
	    int zys=0;  //总页数
	    int pageIndex=(page-1)*rows;//记录数
		try{	
			if("admin".equals(user.getTel())){
			  builds=  buildService.selectAll();
			}else{
			  builds=  buildService.selectAlll(user.getTel());
			}
			  zjls=builds.size();
			  zys = zjls % rows ==0?  zjls/rows:zjls/rows+1;
			  request.setAttribute("zys", zys);
			  if(page>zys){
				page= zys;
			  }
		      request.setAttribute("page", page);
		      request.setAttribute("rows", rows);
			  request.setAttribute("zjls",zjls);
		}catch (Exception e) {
			e.printStackTrace();
		}		   
		    int c=0;
			if(pageIndex+rows>builds.size()){
			 	c=builds.size();
		    }else{
			    c=pageIndex+rows;
		    }
			if(builds.size()%rows==0){
			    if(pageIndex>=builds.size()){
				    pageIndex=c-rows;
			    } 
		    }else{
			    if(pageIndex>=builds.size()){
			    	System.out.println("总页数"+zys);
			    	System.out.println(c);
				    pageIndex=(zys-1)*rows;
				    System.out.println("其页数"+pageIndex);
				} 
			}
			if((c-pageIndex)!=5){
	        	 System.out.println("什么鬼");
				 System.out.println("什么鬼"+zys);
				}
			request.setAttribute("zyss", c-pageIndex);

			//request.setAttribute("zys",zjls%rows);
			 System.out.println(pageIndex+"从"+c);//0-5  5-7
	         for(int i=pageIndex;i<c;i++){
	            System.err.println("这是什么"+builds.get(i));
	            return_build.add(builds.get(i));
	        }
	         
			 model1.setViewName("/ht/listBuild.jsp");
			 model1.addObject("buildlist", return_build); 
	     return model1;
	    }	 

}
